package com.tansuo365.test1.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.tansuo365.test1.bean.otherbean.ShengouDetails;
import com.tansuo365.test1.service.excelservice.IAllDatasService;
import com.tansuo365.test1.service.excelservice.WuliaoExcelService;
import com.tansuo365.test1.service.otherservice.impl.ShengouDetailsCacheServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

//listener不能被spring管理,要每次读取excel都要new,然后里面用到spring可以构造方法传进去
//@PropertySource(value = "classpath:excel.properties")
public class UploadSengouDetailsDataListener extends AnalysisEventListener<ShengouDetails> {
    private static final Logger LOGGER =
            LoggerFactory.getLogger(UploadSengouDetailsDataListener.class);

    //每隔2500条存储数据库,实际使用3000条可以,然后清理list,方便内存回收
//    @Value("${onetime.batch.count}")
    private int BATCH_COUNT = 3000;

    List<ShengouDetails> list = new ArrayList<ShengouDetails>();


    //传入的service层,不同的货品类型调用不同的service
    private IAllDatasService datasService;

    private List<ShengouDetails> returnShengouDetailsList;

    private ShengouDetailsCacheServiceImpl shengouDetailsCacheService;

    /**
     * 通用的goodsService,代表着有关于货品价格,货品存储,货品进出口的相关增删改查等服务层
     * 在本类中使用insertBatch方法,做为读取excel的分段导入时的数据保存
     *
     * @param datasService
     */
    public UploadSengouDetailsDataListener(IAllDatasService datasService, ShengouDetailsCacheServiceImpl shengouDetailsCacheService, List<ShengouDetails> returnShengouDetailsList) {
        this.datasService = datasService;
        this.returnShengouDetailsList = returnShengouDetailsList;
        this.shengouDetailsCacheService = shengouDetailsCacheService;
    }

    //每一条数据解析都会来调用 TODO 如果感觉判定阻碍了传入的效率则取消 if saveCode判定
    @Override
    public void invoke(ShengouDetails infoData, AnalysisContext analysisContext) {
        LOGGER.info("解析到一条申购单物料详情数据:{}", JSON.toJSONString(infoData));

        list.add(infoData);

        //达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存.容易OOM
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();//存储完成后清理list
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //这里也要保存数据,确保最后遗留的数据也存储到数据库
        saveData();
        LOGGER.info("所有数据解析保存完成!");
    }

    //save data , when excel data in here
    private void saveData() {
        LOGGER.info("{}条数据，开始存储数据库！", list.size());
        //将datasService的类型从IAllDataService转成WuliaoExcelService
        WuliaoExcelService wuliaoExcelService = (WuliaoExcelService)datasService;
        returnShengouDetailsList = wuliaoExcelService.batchSelectInfoDataByWuliaobianma(list);
        LOGGER.info(">>>>>>>修改后的shengouDetailsList:"+returnShengouDetailsList+"<<<hashcode>>>"+returnShengouDetailsList.hashCode());
        shengouDetailsCacheService.insertAll(returnShengouDetailsList);
        LOGGER.info("存储数据库成功！");
    }
}
